﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * 供Lua端在运行时动态添加的支持LOD的层级容器
 ***********************************************************************************/

using XLua;
using WorldMapBase.Layer;
using WorldMapBase.LOD;

namespace WorldMapBase
{
    /// <summary>
    /// 供Lua端在运行时动态添加的支持LOD的层级容器
    /// </summary>
    public class LuaLODLayerContainer : LayerContainer, ILODListener
    {
        /// <summary>
        /// C#触发LOD等级变化时调用Lua的方法名。传参为(int currentLodLevel, int lastLodLevel)
        /// </summary>
        public static string FunctionName_OnLODLevelChange = "OnLODLevelChange";

        /// <summary>
        /// C#触发LOD百分比变化时调用Lua的方法名。传参为(int lodLevel, int nextLevelPercent)
        /// </summary>
        public static string FunctionName_OnNextLODPercentUpdate = "OnNextLODPercentUpdate";

        /// <summary>
        /// C#触发容器轮询时调用Lua的方法名
        /// </summary>
        public static string FunctionName_OnUpdate = "OnUpdate";

        private LuaTable m_LuaTable;
        private LuaFunction m_LuaFunction_OnLODLevelChange;
        private LuaFunction m_LuaFunction_OnNextLODPercentUpdate;
        private LuaFunction m_LuaFunction_OnUpdate;

        public LuaLODLayerContainer(LuaTable luaTable)
        {
            m_LuaTable = luaTable;
            m_LuaFunction_OnLODLevelChange = m_LuaTable.Get<LuaFunction>(FunctionName_OnLODLevelChange);
            m_LuaFunction_OnNextLODPercentUpdate = m_LuaTable.Get<LuaFunction>(FunctionName_OnNextLODPercentUpdate);
            m_LuaFunction_OnUpdate = m_LuaTable.Get<LuaFunction>(FunctionName_OnUpdate);
        }

        public void OnLODLevelChange(int currentLodLevel, int lastLodLevel)
        {
            m_LuaFunction_OnLODLevelChange?.Call(currentLodLevel, lastLodLevel);
        }

        public void OnNextLODPercentUpdate(int lodLevel, float nextLevelPercent)
        {
            m_LuaFunction_OnNextLODPercentUpdate?.Call(lodLevel, nextLevelPercent);
        }

        protected override void OnUpdate()
        {
            base.OnUpdate();
            m_LuaFunction_OnUpdate?.Call();
        }
    }
}


